package com.aaa;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

public class BloomFilterCase {

    /**
     * 大小 预计插入数据
     */
    private static final int size = 1000000;


    /**
     * fpp 期望的误判率
     */
    private static final double fpp = 0.01;


    /**
     * 布隆过滤器
     */
    private static final BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size, fpp);

    public static void main(String[] args) {
        int total = 1000000;
        for (int i = 0; i < total; i++) {
            bloomFilter.put(i);
        }

        // 用另外10w测试数据，测试误判率
        int count = 0;
        for (int i = total; i < total + 100000; i++) {
            if(bloomFilter.mightContain(i)) {
                count++;
            }
        }
        System.out.println("总共误判数：" + count);
        System.out.println("误判率：" + (1.0 * count / 100000));
    }
}
